package com.movie.modules.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.movie.common.utils.PageUtils;
import com.movie.common.utils.R;
import com.movie.modules.api.service.AppService;
import com.movie.modules.jf.entity.*;
import com.movie.modules.jf.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JF-PJJ
 */
@Service
@Transactional
public class AppServiceImpl implements AppService {

    @Autowired
    private MovieService movieService;
    @Autowired
    private ActorService actorService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private BannerService bannerService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private MovieCommentService commentService;
    @Autowired
    private RelationService relationService;
    @Autowired
    private VipService vipService;

    @Override
    public R homeSearch(String s) {
        Map<String,Object> map=new HashMap<>();
        List<MovieEntity> movieList=movieService.list(new QueryWrapper<MovieEntity>().like("name",s).last("limit 5").orderByDesc("release_date"));
        map.put("movieList",movieList);
        List<ActorEntity> actorList=actorService.list(new QueryWrapper<ActorEntity>().like("actor_name",s).last("limit 5").orderByDesc("actor_id"));
        map.put("actorList",actorList);
        List<NewsEntity> newsList=newsService.list(new QueryWrapper<NewsEntity>().like("title",s).last("limit 5").orderByDesc("release_date"));
        map.put("newsList",newsList);
        return R.ok().put("data",map);
    }

    @Override
    public List<BannerEntity> bannerList(int type) {
        return bannerService.list(new QueryWrapper<BannerEntity>().eq("status",1).eq("type",type).orderByDesc("sort_num").last("limit 10"));
    }

    @Override
    public List<TypeEntity> typeList(Integer type,Integer n) {
        return typeService.list(new QueryWrapper<TypeEntity>().eq("status",1).eq("type",type).orderByDesc("type_id").last(n!=null,"limit "+n));
    }

    @Override
    public List<MovieEntity> hotMovieList() {
        return movieService.getHotMovieList(15);
    }

    @Override
    public PageUtils movieListPage(Integer page,Integer typeId) {
        Map<String,Object> params=new HashMap<>();
        params.put("page",page);
        params.put("limit",15);
        return movieService.getMovieByTypeId(params,typeId);
    }

    @Override
    public R movieDetail(Integer movieId) {
        Map<String,Object> map=movieService.getInfoById(movieId);
        MovieEntity movie= (MovieEntity) map.get("movie");
        if(movie==null||movie.getStatus()==0){
            map=null;
        }
        return R.ok().put("data",map);
    }

    @Override
    public PageUtils movieCommentList(Integer movieId,Integer page) {
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",15);
        return commentService.getCommentByMovieId(map,movieId);
    }

    @Override
    public R actorDetail(Integer actorId) {
        return R.ok().put("data",actorService.getInfoById(actorId));
    }

    @Override
    public void collect(RelationEntity relation) {
        if(relation.getCollect()==1){
            relationService.save(relation);
        }else{
            relationService.remove(new QueryWrapper<RelationEntity>().eq("resource_id",relation.getResourceId()).eq("relation_id",relation.getRelationId()).eq("relation_type",relation.getRelationType()));
        }
    }

    @Override
    public R movieVip(Integer movieId, Integer vipId) {
        List<MovieEntity> list=movieService.getMovieVip(movieId, vipId);
        Map<String,Integer> map=new HashMap<>();
        map.put("like",list.size());
        return R.ok().put("data",map);
    }

    @Override
    public R actorVip(Integer actorId, Integer vipId) {
        List<ActorEntity> list=actorService.getActorVip(actorId,vipId);
        Map<String,Integer> map=new HashMap<>();
        map.put("like",list.size());
        return R.ok().put("data",map);
    }

    @Override
    public PageUtils movieVipList(Integer page, Integer vipId) {
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",15);
        return movieService.getMovieVipList(map,vipId);
    }

    @Override
    public PageUtils actorVipList(Integer page, Integer vipId) {
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",15);
        return actorService.getActorVipList(map,vipId);
    }

    @Override
    public void saveComment(MovieCommentEntity comment) {
        commentService.save(comment);
    }

    @Override
    public R vipDetail(Integer vipId) {
        return R.ok().put("data",vipService.getById(vipId));
    }

    @Override
    public void updateVip(VipEntity vip) {
        vipService.updateById(vip);
    }

    @Override
    public void deleteComment(Integer commentId) {
        commentService.removeById(commentId);
    }

    @Override
    public PageUtils commentList(Integer page, Integer vipId) {
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",15);
        return commentService.getCommentByVipId(map,vipId);
    }

    @Override
    public PageUtils newsList(Integer page) {
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",15);
        map.put("status",1);
        return newsService.queryPage(map);
    }

    @Override
    public R newsDetail(Integer newsId) {
        return R.ok().put("data",newsService.getInfoById(newsId));
    }

}
